<?php
namespace app\gr\controller;

use app\gr\BLL\CardRedeemBLL;
use cmf\controller\AdminBaseController;

class AdminCardRedeemController extends AdminBaseController
{
    private $bll;
    private $ControlName = "AdminCardRedeem";

    public function __construct(CardRedeemBLL $cardRedeem)
    {
        parent::__construct();
        $this->bll = $cardRedeem;
    }

    public function index()
    {
        $params = $this->request->param();

        $where = [];
        $request = input('request.');
        if (!empty($request['tpname'])) {
            $tpname = $request['tpname'];
            $where['type.name'] = ['like', "%$tpname%"];
        }
        if (!empty($request['card'])) {
            $card = $request['card'];
            $where['pub.card_no'] = ['like', "%$card%"];
        }

        if (!empty($request['name'])) {
            $name = $request['name'];
            $where['a.name'] = ['like', "%$name%"];
        }
        if (!empty($request['tele'])) {
            $tele = $request['tele'];
            $where['a.tele'] = ['like', "%$tele%"];
        }

        $list = $this->bll->getListPage($where);
        $list->appends($params);
        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');
        $columns = 'type.img,type.name tpname,type.spk,a.*';
        $redeem = $this->bll->getbyid($id, $columns);
        $this->assign('redeem', $redeem);
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $id = $this->request->param('id', 0, 'intval');
            $data = $this->request->param();

            if ($this->bll->update($data, $id)) {
                $this->success("更新成功！", url("$this->ControlName/index"));
            } else {
                $this->error('更新失败！');
            }
        }
    }

    public function excel()
    {
        $xlsName = "礼包兑换";
        $xlsCellTitle = array('ID', '姓名', '电话', '地址', '快递编号');

        $columns = 'id,name,tele,address,express_no';

        $where['express_status'] = 0;

        $list = $this->bll->findWhere($where, $columns);

        exportExcel($xlsName, $xlsCellTitle, $list);
    }
}
